package com.comdosoft.comdoandroidbase.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.comdosoft.comdoandroidbase.R;
import com.comdosoft.comdoandroidbase.adapter.LeftSlideDeleteAdapter;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;
/**
 * 
 * PullToRefreshListView
 * ListView 上下均可加入布局，需要用
 * 主要用于含下拉刷新
 * @author niemin
 *
 */
public class PullToRefreshListViewActivity extends Activity implements OnClickListener,
		PullToRefreshBase.OnRefreshListener<ListView> {

	private PullToRefreshListView pullToRefreshListView;

	private LeftSlideDeleteAdapter adapter;
	private List<String> dataList = new ArrayList<String>();

	private int pageindex = 0;
	private DisplayMetrics dm;
	
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				pullToRefreshListView.onRefreshComplete();
				for (int i = 0; i < 10; i++) {
					dataList.add("测试数据"+i);
				}
				adapter.notifyDataSetChanged();
				break;
			case 1:
				pullToRefreshListView.onRefreshComplete();
				for (int i = pageindex*10; i < (pageindex+1)*10; i++) {
					dataList.add("测试数据"+i);
				}
				adapter.notifyDataSetChanged();
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitle("PullToRefreshListView");
		setContentView(R.layout.activity_pulltorefreshlistview);

		dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		
		initView();
	}

	private void initView() {
		pullToRefreshListView = (PullToRefreshListView) findViewById(R.id.pullToRefreshListView);
		/**
		 * PullToRefreshListView添加view
		 */
		View headerView = LayoutInflater.from(this).inflate(R.layout.activity_pull_headerview,null);
		ListView mListView = pullToRefreshListView.getRefreshableView();
		mListView.addHeaderView(headerView);
		
		pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
		pullToRefreshListView.setOnRefreshListener(this);

		for (int i = 0; i < 10; i++) {
			dataList.add("测试数据"+i);
		}

		adapter = new LeftSlideDeleteAdapter(this, dataList,dm.widthPixels,new OnClickListener());
		pullToRefreshListView.setAdapter(adapter);

		pullToRefreshListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				
			}
		});
	}
	private class OnClickListener implements LeftSlideDeleteAdapter.OnClickListener{

		@Override
		public void onNormalItem(int position) {

		}

		@Override
		public void onDelItem(int position) {
			dataList.remove(dataList.get(position));
			adapter.notifyDataSetChanged();
		}


	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		default:
			break;
		}
	}

	@Override
	public void onRefresh(PullToRefreshBase<ListView> refreshView) {
		if (refreshView.isHeaderShown()) {
			pageindex = 0;
			dataList.clear();
			handler.sendEmptyMessage(0);

		}else if (refreshView.isFooterShown()) {
			pageindex++;
			handler.sendEmptyMessage(1);
		}
	}
}
